//
//  ClassDeatilViewController.m
//  CWRD
//
//  Created by lanou on 15/9/18.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "ClassDeatilViewController.h"
#import "TimeTableViewCell.h"
#import "ShareTableViewCell.h"
#import "DetailModel.h"
#import "DetailViewController.h"
#import "WebViewViewController.h"
@interface ClassDeatilViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)HMSegmentedControl *segmentControl;
//按时间排序数组
@property(nonatomic,strong)NSMutableArray *timeArray;
//分享排行榜数组
@property(nonatomic,strong)NSMutableArray *shareArray;

@end

@implementation ClassDeatilViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.timeAndShare);
    self.navigationItem.title = self.classTitle;
    
    //设置segmentControl
    self.segmentControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"按时间排序",@"分享排行榜"]];
    self.segmentControl.selectedIndex = 0;
    [self.segmentControl setFrame:CGRectMake(0, 0, kWidth, 40)];
    self.segmentControl.selectionIndicatorColor = [UIColor cyanColor];
    self.segmentControl.selectionIndicatorHeight = 2.0f;
    self.segmentControl.alpha = 0.9;
    self.segmentControl.backgroundColor = [UIColor blackColor];
    self.segmentControl.textColor = [UIColor whiteColor];
    self.segmentControl.font = [UIFont fontWithName:@"FZLTZCHJW--GB1-0" size:14.0];
    [self.segmentControl addTarget:self action:@selector(actionSegmentControl:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmentControl];
    
    [self addView];
    [self reloadDataForDetailByTime];
    [self reloadDataForDetailByShare];
    //注册cell
    [self.timeView registerClass:[TimeTableViewCell class] forCellReuseIdentifier:@"timeCell"];
    [self.shareView registerClass:[ShareTableViewCell class] forCellReuseIdentifier:@"shareCell"];
}

- (NSMutableArray *)timeArray{
    if (!_timeArray) {
        _timeArray = [[NSMutableArray alloc] init];
    }
    return _timeArray;
}

#pragma mark--------按时间排序数据解析--------
- (void)reloadDataForDetailByTime{
    [LORequestManger GET:self.timeAndShare[0] success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *videoList in dic[@"videoList"]) {
            DetailModel *model = [DetailModel shareListModelWithVideoList:videoList consumption:videoList[@"consumption"]];
            [self.timeArray addObject:model];
        }
        [self.timeView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}


- (NSMutableArray *)shareArray{
    if (!_shareArray) {
        _shareArray = [[NSMutableArray alloc] init];
    }
    return _shareArray;
}


#pragma mark ----------分享排行榜数据解析------------
- (void)reloadDataForDetailByShare{
    [LORequestManger GET:self.timeAndShare[1] success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *videoList in dic[@"videoList"]) {
            DetailModel *model = [DetailModel shareListModelWithVideoList:videoList consumption:videoList[@"consumption"]];
            [self.shareArray addObject:model];
        }
        [self.shareView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}


//加载视图
- (void)addView{
    self.timeView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kWidth, kHeight - 153) style:UITableViewStylePlain];
    self.timeView.hidden = NO;
    self.timeView.backgroundColor = [UIColor whiteColor];
    self.timeView.delegate = self;
    self.timeView.dataSource = self;
    self.timeView.tag = 10000;
    [self.view addSubview:self.timeView];
    
    self.shareView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kWidth, kHeight -  153) style:UITableViewStylePlain];
    self.shareView.backgroundColor = [UIColor whiteColor];
    self.shareView.hidden = YES;
    self.shareView.delegate = self;
    self.shareView.dataSource = self;
    self.shareView.tag = 10001;
    [self.view addSubview:self.shareView];
    
}


//分别隐藏"按时间排序"与"分享排行榜"
- (void)actionSegmentControl:(HMSegmentedControl *)segmentControl{
    switch (segmentControl.selectedIndex) {
        case 0:
            self.shareView.hidden = YES;
            self.timeView.hidden = NO;
            break;
        case 1:
            self.timeView.hidden = YES;
            self.shareView.hidden = NO;
            break;
        default:
            break;
    }
    
}

//设置分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


//设置一个分区内的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag == 10000) {
        return self.timeArray.count;
    }else{
        return self.shareArray.count;
    }
}


//cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //按时间排序
    if (tableView.tag == 10000) {
        TimeTableViewCell *timeCell = [self.timeView dequeueReusableCellWithIdentifier:@"timeCell"forIndexPath:indexPath];
        DetailModel *model = self.timeArray[indexPath.row];
        timeCell.classLabel.text = [NSString stringWithFormat:@"#%@",model.category];
        timeCell.nameLabel.text = model.title;
        [timeCell.post sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail]];
        
        timeCell.timeLabel.text = [NSString stringWithFormat:@"/ %@",[self setTimeWithInterval:[model.duration floatValue]]];
        return timeCell;
    }else{
        //分享排行榜
        ShareTableViewCell *shareCell = [self.shareView dequeueReusableCellWithIdentifier:@"shareCell"forIndexPath:indexPath];
        DetailModel *model = self.shareArray[indexPath.row];
        shareCell.classLabel.text = [NSString stringWithFormat:@"#%@",model.category];
        shareCell.nameLabel.text = model.title;
        [shareCell.post sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail]];
        
        shareCell.timeLabel.text = [NSString stringWithFormat:@"/ %@",[self setTimeWithInterval:[model.duration floatValue]]];
        return shareCell;
    }
    
    
}

//设置每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

//时长转化成分钟类型
- (NSString *)setTimeWithInterval:(float)interval
{
    //分钟
    int minutes = interval / 60;
    //秒
    int seconds = (int)interval % 60;
    return [NSString stringWithFormat:@"%d'%02d''",minutes,seconds];
}

#pragma mark ---------跳转到详情页面--------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WebViewViewController *webVC = [[WebViewViewController alloc]init];
    if (tableView.tag == 10000) {
        webVC.modelArray = self.timeArray;
       webVC.index = indexPath.row;
    }else{
        webVC.modelArray = self.timeArray;
        webVC.index = indexPath.row;
    }
    [self.navigationController pushViewController:webVC animated:YES];
}

//设置滑动屏幕时tabBar隐藏
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    if (self.timeView.contentOffset.y > 0) {
        
        self.tabBarController.tabBar.hidden = YES;
    }else {
        
        self.tabBarController.tabBar.hidden = YES;
    }

}



//将留白去除
- (void)viewDidLayoutSubviews{
    
    UIEdgeInsets insert = self.timeView.contentInset;
    insert.bottom = 0;
    insert.top = 0;
    self.timeView.contentInset = insert;
    insert = self.timeView.scrollIndicatorInsets;
    insert.bottom = 0;
    insert.top = 0;
    self.timeView.scrollIndicatorInsets = insert;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
